package edu.byui.chat;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import org.junit.Test;

public class ChatTest {
	public Message testMessage = new Message();
	private String FROM = "testSender";
	private String TO = "testReciever";
	private String MESSAGE = "This is a test Message!";
	private GregorianCalendar SENT = new GregorianCalendar(2009,8,1,10,02);

	@Test
	public void testMessageBean() {
		testMessage.setFrom(FROM);
		testMessage.setTo(TO);
		testMessage.setMessage(MESSAGE);
		testMessage.setSent(SENT);
		assertEquals("Checking from attribute storage.", FROM, testMessage.getFrom());
		assertEquals("Checking to attribute storage.", TO, testMessage.getTo());
		assertEquals("Checking message attribute storage.", MESSAGE, testMessage.getMessage());
		assertEquals("Checking the Date storage for sent.", SENT, testMessage.getSent());
	}
	
	@Test
	public void testAddMessageComplete() {
		Chat oChat = new Chat();
		
		testMessage.setFrom(FROM);
		testMessage.setTo(TO);
		testMessage.setMessage(MESSAGE);
		testMessage.setSent(SENT);
		try {
			oChat.addMessage(testMessage);
		}
		catch (Exception e) {
			e.printStackTrace();
			fail("Error while adding the new message!");
		}
	}

	@Test
	public void testGetMessagesFor() {
		testMessage.setFrom(FROM);
		testMessage.setTo(TO);
		testMessage.setMessage(MESSAGE);
		testMessage.setSent(SENT);
		Chat oChat = new Chat();
		Message otherMessage = new Message(-1, "the Other Guy", "you", "some other message", new GregorianCalendar(1986,6,17,10,02), -1);
		oChat.addMessage(testMessage);
		oChat.addMessage(otherMessage);
		ArrayList<Message> receivedList;
		try {
			receivedList = (ArrayList<Message>)oChat.getMessagesFor(TO);
		}
		catch (Exception e) {
			e.printStackTrace();
			fail("Error while getting a message for " + TO + "!");
		}
		
		receivedList = (ArrayList<Message>)oChat.getMessagesFor(TO);

		Message receivedMessage = receivedList.get(0);
		assertEquals("Checking from attribute storage.", FROM, receivedMessage.getFrom());
		assertEquals("Checking to attribute storage.", TO, receivedMessage.getTo());
		assertEquals("Checking message attribute storage.", MESSAGE, receivedMessage.getMessage());
		assertEquals("Checking the Date storage for sent.", SENT, receivedMessage.getSent());
		assertEquals("Checking that there was only one Message", 1, receivedList.size());
	}

	public void testGetData() {
		ArrayList<Object> temp =new ArrayList<Object>();
		temp.add("dockey");
		GetData test = new GetData();
		test.queryTest("SELECT CONCAT('FROM: ', message_from), CONCAT('TO: ', message_to), CONCAT('SENT: ', message_sent), CONCAT('BODY: ', message_body) FROM message WHERE message_to = ?;", temp);
		test.getMessagesFor("dockey");
		temp = new ArrayList<Object>();
		temp.add("root");
		test.queryTest("SELECT COUNT(*) FROM user WHERE user_name = ?;", temp);
	}
	
	public void testSetData() {
		ArrayList<Object> temp =new ArrayList<Object>();
		Message m = new Message("ricky", "dockey", "How's the DateTime?");
		temp.add("bobo");
		temp.add("jimi");
		temp.add(new GregorianCalendar());
		temp.add("You have exceeded your storage capacity!");
		SetData test = new SetData();
		test.addMessage(m);
	}
}
